
package msu.ml.data.level2;

import java.io.*;

/**
 * @author Reginald M Mead
 * @version 1.0
 *
 * The Packet class represents a packet
 * of Nexrad Level II data.
 *
 */
public class MomentDataBlock 
{
   public MomentDataBlock(Level2InputStream stream)
   {
      try
      {
         type = (char)stream.readByte();
         name = new char[3];
         stream.readBytes(name, 0, 3);
         stream.skip(4);
         gates = stream.readShort();
         range = stream.readShort();
         gateSize = stream.readShort();
         tover = stream.readShort();
         snr = stream.readShort();
         cFlags = stream.readByte();
         wordSize = stream.readByte();
         scale = stream.readFloat();
         offset = stream.readFloat();
         if(gates > 0)
         {
            data = new short[gates * (wordSize / 8)];
            byte [] buf = new byte[gates * (wordSize / 8)];
            stream.readBytes(buf, 0, gates * (wordSize / 8));
            for(int i = 0; i < buf.length; i++)
            {
               int temp = (0x000000FF & ((int)buf[i]));
               data[i] = (short)temp;
            }
         }
      }
      catch(IOException e)
      {
         System.out.println("MomentDataBlock Error: " + e.getMessage());
         e.printStackTrace();
      }
   }

   private static String formatString = "\nName: %1$c%2$s\nGates: %3$d\nRange: %4$d\nGate Size: %5$d\nThreshold: %6$d\nSNR: %7$d\nFlags: %8$d\nWord Size %9$d\nScale: %10$.2f\nOffset: %11$.2f";

   public String toString()
   {
      return String.format(MomentDataBlock.formatString, type, getName(), gates,
         range, gateSize, tover, snr, cFlags, wordSize, scale, offset);
   }

   public String getName()
   {
      return new String(name);
   }

   public int blockSize()
   {
      return 28 + (gates * (wordSize / 8));
   }

   public char type;
   public char [] name;
   public short gates;
   public short range;
   public short gateSize;
   public short tover;
   public short snr;
   public byte cFlags;
   public byte wordSize;
   public float scale;
   public float offset;
   public short [] data;
}
